package com.sz.biz.pay.account.service.impl;

import com.sz.biz.common.constants.ErrorCodes;
import com.sz.biz.common.constants.ShortName;
import com.sz.biz.pay.account.entity.Transaction;
import com.sz.biz.pay.account.service.TransactionService;
import com.sz.biz.pay.constants.PayConstant;
import com.sz.common.base.dao.ParamData;
import com.sz.common.base.dto.QResultDto;
import com.sz.common.base.dto.QueryParams;
import com.sz.common.base.exception.Exceptions;
import com.sz.common.base.service.AbstractService;
import com.sz.common.base.utils.CollectionUtils;
import com.sz.common.base.utils.StringUtils;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * Function: 交易流水 <br>
 * @quthor: siqishangshu <br>
 * @date: 2017-11-03 10:49:00
 */
@Service
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class TransactionServiceImpl extends AbstractService implements TransactionService {
    @Override
    public int insert(Transaction entry) {
        return  dao.save(getMapperNamespace()+ ShortName.INSERT,entry);
    }

    @Override
    public int delete(String id) {
        return  dao.delete(getMapperNamespace()+ ShortName.DELETE_BY_ID,id);
    }

    @Override
    public int delete(List ids) {
        return 0;
    }

    @Override
    public int update(Transaction entry) {
        return  dao.update(getMapperNamespace()+ ShortName.UPDATE_BY_ID_SELECTIVE,entry);
    }

    @Override
    public Transaction select(String id) {
        if (StringUtils.isEmpty(id)) {
            throw Exceptions.bizException(ErrorCodes.ERROR_ARGUMENT_EMPTY, "id");
        }
        ParamData pd = new ParamData();
        pd.put("id",id);
        return (Transaction)dao.findForObject(getSqlName("selectByPrimaryKey"), pd);
    }

    @Override
    public List<Transaction> selectList(List ids) {
        return null;
    }

    @Override
    public List<Transaction> selectAll() {
        return null;
    }

    @Override
    public QResultDto selectList(QueryParams param) {
        return null;
    }

    @Override
    public int count(QueryParams param) {
        return 0;
    }

    @Override
    public int countAll() {
        return 0;
    }

    @Override
    public Transaction findByBillNo(String billNo) {

        if (StringUtils.isEmpty(billNo)) {
            throw Exceptions.bizException(ErrorCodes.ERROR_ARGUMENT_EMPTY, "billNo");
        }
        ParamData pd = new ParamData();
        pd.put("billNo",billNo);
        return (Transaction)dao.findForObject(getSqlName("selectByBillNo"), pd);
    }

    @Override
    public Transaction findByThirdLogNo(String thirdLogNo) {

        if (StringUtils.isEmpty(thirdLogNo)) {
            throw Exceptions.bizException(ErrorCodes.ERROR_ARGUMENT_EMPTY, "thirdLogNo");
        }

        ParamData paramData = new ParamData();
        paramData.put("thirdLogNo",thirdLogNo);
        return (Transaction)dao.findForObject(getSqlName("findByThirdLogNo"), paramData);
    }

    @Override
    public QResultDto findByQuery(QueryParams params) {
        ParamData pd = convertQueryParams(params);
        //根据条件获取账单列表
        List<Transaction> transactionlist = dao.findForList(getSqlName("selectListByQueryListPage"),pd,Transaction.class);

        return new QResultDto(transactionlist, pd.getPagination());
    }

    @Override
    public List<Transaction> findByTranStatusAndTranType(int tranStatus, int tranType) {
        ParamData pd = new ParamData();
        pd.put("tranStatus",tranStatus);
        pd.put("tranType", tranType);
        //根据条件获取账单列表
        List<Transaction> transactionlist = dao.findForList(getSqlName("findListByTranStatusAndTranType"),pd,Transaction.class);

        return transactionlist;
    }

    @Override
    public Transaction getTransactionByThirdLogNo(String thirdLogNo) {
        return (Transaction)dao.findForObject(getSqlName("selectByThirdLogNo"),thirdLogNo);
    }

    @Override
    protected String getMapperNamespace() {
        return "com.sz.biz.pay.account.TransactionMapper";
    }

    @Override
    public void batchUpdate(List<String> orderList) {
        if (!CollectionUtils.isEmpty(orderList)) {
            Map<String, Object> map = new HashedMap();
            map.put("tranStatus", PayConstant.TranStatus.SUCCESS);
            map.put("ids", orderList);
            dao.update(getSqlName("updateByOrderNo"), map);
        }
    }

}
